package org.hs.phd.odi.bgsub;

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

import org.hs.phd.common.simpleplayer.ImageSequenceFrame;
import org.hs.phd.common.simpleplayer.Overlay;
import org.hs.phd.common.simpleplayer.PlayerFilter;
import org.hs.phd.odi.bgsub.classification.ODISimpleVehiclePersonClassifier;
import org.hs.phd.odi.bgsub.classification.VisualObjectClassifier;
import org.hs.phd.odi.bgsub.features.FeatureDetector;
import org.hs.phd.odi.bgsub.features.GoodFeaturesToTrackFeatureDetector;
import org.hs.phd.odi.bgsub.features.ODIShapeOverlappingFeatureDetector;
import org.hs.phd.odi.bgsub.features.ORBFeatureDetector;
import org.hs.phd.odi.bgsub.features.SIFTFeatureDetector;
import org.hs.phd.odi.bgsub.features.SURFFeatureDetector;
import org.hs.phd.odi.bgsub.features.ShapeOverlappingFeatureDetector;
import org.hs.phd.odi.bgsub.tracking.DefaultVisualObjectTracker;
import org.hs.phd.odi.bgsub.tracking.ParticleFilterVisualObjectTracker;
import org.hs.phd.odi.bgsub.tracking.VisualObjectTracker;
import org.hs.phd.odi.bgsub.vob.VisualObject;
import org.hs.phd.odi.bgsub.vob.VisualObjectOverlay;
import org.hs.phd.odi.transformation.ODITransformation;

public class FGObjectDetectionFilter implements PlayerFilter {
	
	private static final int FRAMES_NEEDED_FOR_BG_INIT = 100;

	private FGObjectDetector fgObjectDetector;

	private FeatureDetector featureDetector;

	private VisualObjectTracker tracker;

	private VisualObjectClassifier classifier;

	public FGObjectDetectionFilter(ODITransformation transformation) {
		fgObjectDetector = new FGObjectDetector(FRAMES_NEEDED_FOR_BG_INIT);
		//featureDetector = new GoodFeaturesToTrackFeatureDetector();
		//featureDetector = new SURFFeatureDetector();
		//featureDetector = new ShapeOverlappingFeatureDetector();
		featureDetector = new ODIShapeOverlappingFeatureDetector(transformation);
		//featureDetector = new SIFTFeatureDetector();
		//featureDetector = new ORBFeatureDetector();
		//tracker = new DefaultVisualObjectTracker(featureDetector.getFeatureMatcher());
		tracker = new ParticleFilterVisualObjectTracker();
		classifier = new ODISimpleVehiclePersonClassifier(transformation);
	}

	@Override
	public void filter(ImageSequenceFrame input) {
		
		List<VisualObject> fgObjects = fgObjectDetector.detectFGObjects(input);
		
		
		if( fgObjectDetector.isReady() ){
			
			featureDetector.detectAndSetFeatures(input.getFrameImage(), fgObjects);
			
			classifier.classifyObjects(input.getFrameImage(), fgObjects);
			
			List<? extends VisualObject> trackedObjects = tracker.track(fgObjects);
			
		    int objectId = 0;
		    for (VisualObject object: trackedObjects) {
		        input.addOverlay(this, objectId++, new VisualObjectOverlay(object, new Color(255, 0, 0, 100)));	        
		    }
		}else{
			input.addOverlay(this, 0, new Overlay() {
				
				@Override
				public void paint(Graphics g) {
					g.setColor(Color.CYAN);
					g.drawString("Learning background (" + fgObjectDetector.getRemainingFramesForBGInitCompletion() + ") ...", 10, 10);
				}
			});
		}
		
	}

}
